﻿--条件分支if-else
select * from AccountInfo
select * from BankCard
select * from CardExchange
select * from CardStateChange
select * from CardTransfer
--1. 为赵云此人进行开户开卡操作，赵云身份证：420107199904054233
insert into AccountInfo values('420107199904054233','13448756354','赵云','2022-09-22 16:43:00')
--2. 需要求出张飞的银行卡卡号和余额，张飞身份证：420107199602034138  （1.使用连接查询，2.使用变量）
select CardNo,CardMoney from BankCard where AccountId=(select AccountId from AccountInfo where AccountCode='420107199602034138')
--某用户银行卡号为“6225547854125656”，该用户执行取钱操作，取钱5000元，余额充足则进行取钱操作，
--并提示"取钱成功"，否则提示“余额不足”。
declare @CardMoney money
select @CardMoney = CardMoney from BankCard where CardNo = '6225547854125656'
if (@CardMoney >= 5000)
begin
	print '取钱成功'
end
else
begin
	print '取钱失败'
end

--条件分支:case-when


--查询银行卡信息，将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”，
select * ,
case 
	when CardState = 1 then '正常'
	when CardState = 2 then '挂失'
	when CardState = 3 then '冻结'
	when CardState = 4 then '注销'
end as 状态
from BankCard

--并且根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户"，

select * ,
case 
	when CardMoney <= 300000 then '普通用户'
	when CardMoney >= 500000 then 'vip用户'
end as 等级
from BankCard

--显示列分别为卡号，身份证，姓名，余额，用户等级，银行卡状态。
 
select CardNo,RealName,CardMoney,
case 
	when CardState = 1 then '正常'
	when CardState = 2 then '挂失'
	when CardState = 3 then '冻结'
	when CardState = 4 then '注销'
end as 用户等级,
case 
	when CardMoney <= 300000 then '普通用户'
	when CardMoney >= 500000 then 'vip用户'
end as 银行卡状态
from AccountInfo join BankCard on BankCard.AccountId = AccountInfo.AccountId 
--**while**

--循环打印1-10。
declare @a int = 1
while @a < 10
	begin
		print @a
		set @a = @a + 1
	end
print @a
--​	6.打印99乘法表
declare @i int = 1
while @i <= 9
	begin
		declare @j int = 1
		declare @result varchar(200) = ''
		while  @j <= @i
			
			begin
				set @result = @result + cast(@i as varchar(1))+ ' * ' + cast(@j as varchar(1)) + ' = '+ cast(@i*@j as varchar(2)) + char(9)
				set @j = @j + 1
			end
		print @result
		set @i = @i + 1
	end